package cn.stylefeng.guns.modular.time.controller;

import cn.stylefeng.guns.modular.time.entity.AppUser;
import cn.stylefeng.guns.modular.time.model.AppUserRequest;
import cn.stylefeng.guns.modular.time.service.AppUserService;
import cn.stylefeng.roses.kernel.auth.api.AuthServiceApi;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.payload.DefaultJwtPayload;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.ErrorResponseData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@ApiResource(name = "APP用户", path = "/appUser")
public class AppUserController {

    private final AppUserService appUserService;

    public AppUserController(AppUserService appUserService) {
        this.appUserService = appUserService;
    }

    @Resource
    private AuthServiceApi authServiceApi;

    @PostResource(name = "登录", path = "/login", requiredLogin = false, requiredPermission = false)
    public ResponseData login(@RequestBody @Validated AppUserRequest request) {
        AppUser user = appUserService.login(request);
        if (user == null) {
            return new ErrorResponseData("400", "账号或密码错误");
        }

        // 构造最简的JWT负载（仅包含用户ID与账号）
        DefaultJwtPayload payload = new DefaultJwtPayload();
        try {
            // 可能存在不同的字段命名，这里尽量设置常用字段
            // 如果DefaultJwtPayload没有对应setter，请根据实际类结构调整
            payload.setUserId(user.getUserId());
            payload.setAccount(user.getAccount());
        } catch (Exception ignored) {
            // 回退：如果payload组装失败，仍然返回用户信息
        }

        // 兼容处理：不在此处主动生成token，由客户端调用 /auth/login 获取
        String token = null;

        Map<String, Object> result = new HashMap<>();
        result.put("user", user);
        result.put("token", token);
        return new SuccessResponseData(result);
    }

    @PostResource(name = "注册", path = "/register", requiredLogin = false, requiredPermission = false)
    public ResponseData register(@RequestBody @Validated AppUserRequest request) {
        AppUser user = appUserService.register(request);
        if (user == null) {
            return new ErrorResponseData("400", "账号已存在");
        }
        return new SuccessResponseData(user);
    }

    @PostResource(name = "新增", path = "/add")
    public ResponseData add(@RequestBody @Validated AppUserRequest request) {
        appUserService.add(request);
        return new SuccessResponseData();
    }

    @PostResource(name = "编辑", path = "/edit")
    public ResponseData edit(@RequestBody @Validated AppUserRequest request) {
        appUserService.edit(request);
        return new SuccessResponseData();
    }

    @PostResource(name = "删除", path = "/delete")
    public ResponseData delete(@RequestBody @Validated AppUserRequest request) {
        appUserService.delete(request);
        return new SuccessResponseData();
    }

    @GetResource(name = "详情", path = "/detail")
    public ResponseData detail(AppUserRequest request) {
        return new SuccessResponseData(appUserService.detail(request));
    }

    @GetResource(name = "分页", path = "/page")
    public ResponseData page(AppUserRequest request) {
        Page<AppUser> page = appUserService.findPage(request);
        return new SuccessResponseData(page);
    }
}